package com.ct.boss.demo.service.impl;

import com.ct.boss.demo.dao.SysUserMapper;
import com.ct.boss.demo.exception.BusinessException;
import com.ct.boss.demo.http.HttpStatus;
import com.ct.boss.demo.model.SysUser;
import com.ct.boss.demo.model.UserRole;
import com.ct.boss.demo.page.PageRequest;
import com.ct.boss.demo.page.PageResult;
import com.ct.boss.demo.utils.PageUtils;
import com.ct.boss.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ChenTong
 * @version 1.0
 * @description 用户管理业务接口实现类
 * @date 2020/6/15 17:47
 * @modified
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper userMapper;

    @Override
    public SysUser login(SysUser user) {
        try {
            if (user==null||user.getUsername()==null||user.getPassword()==null)
                return null;
            SysUser realUser = userMapper.selectByUserName(user.getUsername());
            if(realUser.getPassword().equals(user.getPassword())){
                return realUser;
            }else {
                return null;
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"登录失败",e);
        }

    }

    /**
     * @description 分页查询
     * @author ChenTong
     * @param pageRequest
     * @return com.ct.boss.demo.page.PageResult
     * @date 2020/6/15 8:17
     */
    @Override
    public PageResult findPage(PageRequest pageRequest) {
        try {
            return PageUtils.getPageResult(getPageInfo(pageRequest));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"分页查询失败",e);
        }

    }

    @Override
    public int save(SysUser record) {
        if (record==null) return -1;
        try {
            return userMapper.insert(record);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"添加用户失败");
        }
    }

    @Override
    public int delete(Long id) {
        if (id==null) return -1;
        try {
            return userMapper.deleteByPrimaryKey(id);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"删除用户失败");
        }
    }

    @Override
    public int delete(List<SysUser> records) {
        return 0;
    }

    @Override
    public SysUser findById(Long id) {
        if (id==null) return null;
        try {
            return userMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"删除用户失败");
        }
    }

    @Override
    public int update(SysUser record) {
        if (record==null) return -1;
        try {
            return userMapper.updateByPrimaryKey(record);
        }catch (Exception e){
            log.info(e.getMessage(),e);
            throw new BusinessException(HttpStatus.BUSINESS_ERROR.getCode(),"更新用户失败");
        }
    }

    /**
     * 调用分页插件完成分页
     * @param
     * @return
     */
    private PageInfo<UserRole> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        String query = pageRequest.getQuery();
        PageMethod.startPage(pageNum, pageSize);
        List<UserRole> userRoles =userMapper.selectPage(query);
        return new PageInfo<>(userRoles);
    }
}
